package cn.sinata.wanglin.neighbor.activity;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.xilada.xldutils.activitys.RecyclerListActivity;
import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.DensityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;

import java.util.ArrayList;

import cn.sinata.wanglin.R;
import cn.sinata.wanglin.adapter.SearchUserAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.NewFriend;
import cn.sinata.wanglin.model.SearchUser;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.village.activity.AllUserInfoActivity;
public class AddContactActivity extends RecyclerListActivity {

    private EditText titleView;
    private SearchUserAdapter searchUserAdapter;
    private ArrayList<SearchUser> mFriends = new ArrayList<>();
    private String userId;
    private String userAccount;

    @Override
    protected void initView() {
        super.initView();
        setCanEditable(true);
        titleView = getTitleView();
        titleView.setHint("开心网邻ID/手机号");
        titleView.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
        titleView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,14);
        titleView.setBackgroundResource(R.drawable.search_bg);
        titleView.setTextColor(ContextCompat.getColor(this,R.color.textColor));
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) titleView.getLayoutParams();
        params.topMargin = DensityUtil.dip2px(this,8);
        params.bottomMargin = DensityUtil.dip2px(this,8);
        addRightButton("搜索", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userAccount = titleView.getText().toString().trim();
                if (TextUtils.isEmpty(userAccount)) {
                    showToast("请输入正确的开心网邻ID/手机号");
                    return;
                }
                searchUser();
            }
        });
        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
    }

    @Override
    protected RecyclerView.Adapter setAdapter() {
        searchUserAdapter = new SearchUserAdapter(mFriends);
        searchUserAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                ActivityUtil.create(mContext)
                            .go(AllUserInfoActivity.class)
                            .put("friendID",mFriends.get(position).getId())
                            .startForResult(16);
            }
        });
        return searchUserAdapter;
    }

    private void searchUser(){
        HttpManager.searchUser(userId, userAccount)
                .subscribe(new ResultDataSubscriber<ArrayList<SearchUser>>(this) {
                    @Override
                    public void onSuccess(String msg, ArrayList<SearchUser> friends) {
                        mFriends.clear();
                        if (friends.size() == 0) {
                            setLoadMoreText("没有找到该用户");
                            searchUserAdapter.notifyDataSetChanged();
                            return;
                        }
                        mFriends.addAll(friends);
                        searchUserAdapter.notifyDataSetChanged();
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            //添加好友
            if (requestCode == 16) {
                searchUser();
            }
        }
    }

    @Override
    protected void pullDownRefresh() {
        super.pullDownRefresh();
        searchUser();
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        setRefreshing(false);
    }
}